package com.lwl.service.impl.sas;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.lwl.dao.sas.TSasCallbackMapper;
import com.lwl.entity.MyPage;

import com.lwl.entity.sas.TSasCallback;
import com.lwl.entity.Tuser;

import com.lwl.service.sas.TSasCallbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service

public class TSasCallbackServicelmpl implements TSasCallbackService {
    @Autowired
    private TSasCallbackMapper tSasCallbackDao;

    @Override
    public PageInfo queryAll(MyPage myPage, TSasCallback tSasCallback) {
        PageHelper.startPage(myPage.getPage(),myPage.getLimit());
        List<TSasCallback> tSasCallbackList = tSasCallbackDao.selectAll();
        PageInfo pageInfo = new PageInfo<>(tSasCallbackList);
        return pageInfo;
    }

    @Override
    public int insertCallback(TSasCallback tSasCallback, HttpSession session) {
        int insert = -1;
        if (tSasCallback.getId()==null){
            Integer id = tSasCallback.getId();
            TSasCallback tSasCallback1 = tSasCallbackDao.selectByPrimaryKey(id);
            if (tSasCallback1==null){
                tSasCallback.setCreatetime(new Date());
                insert = tSasCallbackDao.insert(tSasCallback);
            }else {
                tSasCallback.setUpdatetime(new Date());
                Tuser user = (Tuser)session.getAttribute("user");
                String username = user.getUsername();
                tSasCallback.setUpdateby(username);
                insert = tSasCallbackDao.updateByPrimaryKeySelective(tSasCallback);
            }
        }else {
            tSasCallback.setUpdatetime(new Date());
            Tuser user = (Tuser)session.getAttribute("user");
            String username = user.getUsername();
            tSasCallback.setUpdateby(username);
            insert = tSasCallbackDao.updateByPrimaryKeySelective(tSasCallback);
        }
        return insert;
    }

}
